package com.aks.server.service.impl;

import com.aks.server.config.ActivityPriceConfig;
import com.aks.server.domain.request.param.ActivityParam;
import com.aks.server.service.ActivityPriceCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 活动价格计算实现类
 */
@Service
public class ActivityPriceCalculatorImpl implements ActivityPriceCalculator {

    @Autowired
    private ActivityPriceConfig priceConfig;

    @Override
    public BigDecimal calculatePrice(ActivityParam activityParam) {
        // 1. 基础价格
        BigDecimal totalPrice = priceConfig.getBasePrice();

        // 2. 计算人数增加的价格
        totalPrice = totalPrice.add(calculatePeoplePrice(activityParam.getTotalPeople()));

        // 3. 计算天数增加的价格
        totalPrice = totalPrice.add(calculateDaysPrice(activityParam.getActivityDays()));

        // 4. 计算任务数增加的价格
        totalPrice = totalPrice.add(calculateTasksPrice(activityParam.getTasks() != null ? activityParam.getTasks().size() : 0));

        // 5. 计算队伍数增加的价格
        totalPrice = totalPrice.add(calculateTeamsPrice(activityParam.getTeamCount()));

        // 保留两位小数
        return totalPrice.setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 计算人数增加的价格
     */
    private BigDecimal calculatePeoplePrice(Integer totalPeople) {
        if (totalPeople <= priceConfig.getPeople().getBaseCount()) {
            return BigDecimal.ZERO;
        }
        int extraPeople = totalPeople - priceConfig.getPeople().getBaseCount();
        int incrementCount = priceConfig.getPeople().getIncrementCount();
        int increments = (extraPeople + incrementCount - 1) / incrementCount; // 向上取整
        return priceConfig.getPeople().getIncrementPrice().multiply(new BigDecimal(increments));
    }

    /**
     * 计算天数增加的价格
     */
    private BigDecimal calculateDaysPrice(Integer activityDays) {
        if (activityDays <= priceConfig.getDays().getBaseDays()) {
            return BigDecimal.ZERO;
        }
        int extraDays = activityDays - priceConfig.getDays().getBaseDays();
        return priceConfig.getDays().getIncrementPrice().multiply(new BigDecimal(extraDays));
    }

    /**
     * 计算任务数增加的价格
     */
    private BigDecimal calculateTasksPrice(Integer taskCount) {
        if (taskCount <= priceConfig.getTasks().getBaseCount()) {
            return BigDecimal.ZERO;
        }
        int extraTasks = taskCount - priceConfig.getTasks().getBaseCount();
        int incrementCount = priceConfig.getTasks().getIncrementCount();
        int increments = (extraTasks + incrementCount - 1) / incrementCount; // 向上取整
        return priceConfig.getTasks().getIncrementPrice().multiply(new BigDecimal(increments));
    }

    /**
     * 计算队伍数增加的价格
     */
    private BigDecimal calculateTeamsPrice(Integer teamCount) {
        if (teamCount <= priceConfig.getTeams().getBaseCount()) {
            return BigDecimal.ZERO;
        }
        int extraTeams = teamCount - priceConfig.getTeams().getBaseCount();
        int incrementCount = priceConfig.getTeams().getIncrementCount();
        int increments = (extraTeams + incrementCount - 1) / incrementCount; // 向上取整
        return priceConfig.getTeams().getIncrementPrice().multiply(new BigDecimal(increments));
    }
} 